/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package com.techshop.dao.impl;

import com.techshop.dao.ColorDao;
import com.techshop.model.Color;
import java.util.List;
import org.hibernate.Query;
import org.springframework.dao.DataAccessResourceFailureException;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;
import redis.client.ValidatePhonePubSubClient;

/**
 *
 * @author Hai-PC
 */
public class ColorDaoImpl extends HibernateDaoSupport implements ColorDao
{
    @Override
    public Color getById(String colorId)
    {
        try
        {
            return (Color) getSession().get(Color.class, colorId);
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public boolean insert(Color color)
    {
        try
        {
            if (getById(color.getId()) != null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().save(color);
                ValidatePhonePubSubClient.changeColor();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }
    
    @Override
    public boolean update(Color color)
    {
        try
        {
            if (getById(color.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().update(getHibernateTemplate().merge(color));
                ValidatePhonePubSubClient.changeColor();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public boolean delete(Color color)
    {
        try
        {
            if (getById(color.getId()) == null)
            {
                return false;
            }
            else
            {
                getHibernateTemplate().delete(getHibernateTemplate().merge(color));
                ValidatePhonePubSubClient.changeColor();
                return true;
            }
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return false;
        }
    }

    @Override
    public List<Color> getAll()
    {
        try
        {
            return getSession().createQuery("FROM Color").list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }

    @Override
    public List<Color> getAllExceptPhoneId(int phoneId)
    {
        try
        {
            String sql = "select * from color c " +
                            "where c.id not in (select color_id from phone_code where phone_id = :phoneId)";
            Query results = getSession().createSQLQuery(sql).addEntity(Color.class);
            return results.setParameter("phoneId", phoneId).list();
        }
        catch (DataAccessResourceFailureException e)
        {
            System.err.printf("Error: ", e.toString());
            return null;
        }
    }
}